home *** CD-ROM | disk | FTP | other *** search
- Path: news.mindspring.com!usenet
- From: rudd@mindspring.com (Justin Rudd)
- Newsgroups: comp.lang.c++
- Subject: Re: Memory Leak?
- Date: Tue, 12 Mar 1996 05:14:49 GMT
- Organization: MindSpring Enterprises
- Message-ID: <4i31bk$s9c@B1FF.mindspring.com>
- References: <4i2tr9$q6k@newsgate.dircon.co.uk>
- NNTP-Posting-Host: rudd.mindspring.com
- X-Newsreader: Forte Free Agent v0.55
-
- topher@dircon.co.uk (Chris Pyman) wrote:
-
- >Hi all
-
- >Does anybody know whether the following would lead to a memory leak?
- >(Assume this is a Win32 Console App written with Visual C++ v2.0)
-
-
- >main(int argc, char* argv[]) {
- > CString s1("31-12-99");
- > CString s2;
-
- > s2 = s1.Mid(0,2) + s1.Mid(3,2) + s1.Mid(6,2);
-
- > cout << s2;
- > return 0;
- >}
-
-
- Nope...it sure won't.
-
- >Obviously the idea is to set s2 to "311299", but I would have thought
- >that the Mid() method creates a new CString object and returns a
- >reference to it, in which case what becomes of the three "temporary"
- >objects used to build the string in s2? Are they just floating around
- >with no way to get at them, or do they get properly deleted? And if
- >the latter, how does MFC manage it?
-
- The CString::Mid() does create a new CString but its scope is to that
- of the function. So like any variable ( int, float, char, etc... ).
- When its scope is gone...it is gone. I know this isn't the proper way
- to say it...but it explains it clearly enough.
-
- A better example is probably like this:
-
- int Function()
- {
- return 10;
- }
-
- cout << Function();
-
- This creates a temporary int that gets dumped to the screen. The
- concept is the same with a CString.
-
- As for how MFC manages it...I assume you mean how they manage the
- Mid() function and not the string inside. But for the Mid() all they
- do is probably something like this:
-
- CString CString::Mid( int first, int count )
- {
- //not showing selection code because I don't want to get flamed for
- //not being right :-)
- //and the next part is TRULY speculation in my opinion
-
- return CString( /*whatever is holding selection here*/ );
- //or return object if they created a temp CString to hold the
- //selection
- }
-
- Like I said in the comments...this is just _MY OPINION_ if it doesn't
- agree with what anybody who is reading this post thinks sorry :-).
-
- >The main reason I'm asking this is because I want to implement a
- >string class of my own, and would like to be able to use it to build
- >strings as simply as shown above, but I don't want loads of stray
- >objects floating around.
-
- Have fun...
-
- [snip]
-
- >Looking forward to hearing from you
- >Sorry if I'm being ignorant or dimwitted, but I've only read as much
- >of Stroustrup's book as my puny brain can take in at the moment.
-
- Dude...don't be so worried about what people think...you'll get an
- ulcer ;-). Besides I should be worried that someone won't agree with
- my post and I'll get e-mail bombed for being an ignorant MFC
- programmer.
-
- >l+k
- >chris
-
- BTW, all flames are welcome ;-)
-
- Justin Rudd
- rudd@mindspring.com
-
-
-
-